<?php
namespace app\weapp\controller;
use Throwable;
use think\Response;
use app\BaseController;
use EasyWeChat\Work\Application;
use app\weapp\library\WeAppApi;
use Symfony\Component\HttpFoundation\Request as symfonyRequest;
use think\facade\Log;
use app\common\model\Member;
use app\common\library\ImageBind;
class Index extends BaseController
{
    protected array $noNeedLogin = ['image'];
    public function initialize(): void
    {
        parent::initialize();
    }

    public function index()
    {
        $config = [
            'corp_id'=>'ww5d7b19c3c1ce8e1f',
            'secret' => 'Z7kR0_MhnYYq1VRC_6TfPGIiOnsBsyHw9s5_bdt9WnM',
            'token' => 'DGJ25KSmpioWpC12PfOnnLv',
            'aes_key' => '5Aot5xz53NdLsgtzoILpVDtytsYW4f5AgDOYekKdL5l',
            
        ];
          $app = new Application($config);
          $params = $this->request->param();
          $request = symfonyRequest::create(
            $this->request->url(true),
            $this->request->method(),
            $this->request->param(),
            $this->request->cookie(),
            [],
            $this->request->server(),
            $this->request->getContent()?:null
          );
          $app->setRequestFromSymfonyRequest($request);
          $server = $app->getServer();
          Log::write('sync_param:'.json_encode($this->request->param()),'info');
          Log::write('sync_rawinput:'.$this->request->getContent(),'info');
          
        if (empty($params['echostr'])){
                //$message = $server->getRequestMessage();
            $message = $server->getDecryptedMessage();
            Log::write('message:'.json_encode($message),'info');
            if($message['MsgType'] == 'event' && $message['Event'] == 'change_contact'){
                switch ($message['ChangeType']) {
                    case 'create_user':
                      //$api = new  WeAppApi(['agent_id'=>'1000006','secret' => 'vJLtbFBElziZAc0HFj8CPw_sL9nPAVfHSmMXUbNTphw']);
                      //$result = $api->syncHrUser($message['UserID']);
                      $member = new Member();
                      $member->user_id = $message['UserID'];
                      $member->department_id = $message['Department'];
                      /*if($result !== false){
                          if(isset($result['field_info']) && $result['field_info'] ){
                              foreach ($result['field_info'] as $key =>$value){
                                 if($value['result'] == 1){
                                    $member['form_'.$value['fieldid']] = $value['value_type']==1?$value['value_string']:$value['value_uint32'];
                                 }else{
                                    $member['form_'.$value['fieldid']] = '';
                                 }
                              }
                          }
                      }*/
                      $member->save();
                      # code...
                      break;
                    case 'update_user':
                      //$api = new  WeAppApi(['agent_id'=>'1000006','secret' => 'vJLtbFBElziZAc0HFj8CPw_sL9nPAVfHSmMXUbNTphw']);
                      //$result = $api->syncHrUser($message['UserID']);
                      $member = Member::getByUserId($message['UserID']);
                      $member->department_id = $message['Department'];
                      /*if($result !== false){
                          if(isset($result['field_info']) && $result['field_info'] ){
                              foreach ($result['field_info'] as $key =>$value){
                                 if($value['result'] == 1){
                                    $member['form_'.$value['fieldid']] = $value['value_type']==1?$value['value_string']:$value['value_uint32'];
                                 }else{
                                    $member['form_'.$value['fieldid']] = '';
                                 }
                              }
                          }
                      }*/
                      $member->save();
                      break;
                    case 'delete_user':
                      $member = Member::getByUserId($message['UserID']);
                      $member->delete();
                      break;
                    case 'create_party':
                      # code...
                      break;
                    case 'update_party':
                      # code...
                      break;
                    case 'delete_party':
                      # code...
                      break;
                }
            }
        }
        $response =  $server->serve();
        return $response;
    }
     public function image(){
        ImageBind::bulidImage();
    }
}